#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#       copybooloaderfiles
#  
#       Author: weiketing <weikting@gmail.com>
#  

. config.py

copy_to()
{
    grub_targets="x86_64-efi i386-pc"
    grub_libdir=/usr/lib/grub
    grub_pf2=/usr/share/grub/*.pf2

    for d in ${grub_targets}
    do
        mkdir -p ${ISO_ROOT}${GRUB2_PREFIX}
        cp -ra ${grub_libdir}/${d} ${ISO_ROOT}${GRUB2_PREFIX}
    done
    cp -ra ${grub_pf2} ${ISO_ROOT}${GRUB2_PREFIX}
    cp -ra ${grub_libdir}/i386-pc/g2ldr.mbr ${ISO_ROOT}${GRUB2_PREFIX}

    mkdir -p ${ISO_ROOT}/isolinux
    cp -va /usr/lib/syslinux/isolinux.bin ${ISO_ROOT}/isolinux
    if test -d "grub4dos";then
        rm -rf ${ISO_ROOT}/grub4dos
        mkdir -p ${ISO_ROOT}/grub4dos
        cp -L -ar grub4dos/* ${ISO_ROOT}/grub4dos
        ln -f ${ISO_ROOT}/grub4dos/{grldr,grldr.mbr} ${ISO_ROOT}/
        test -e menu.lst && cp -va menu.lst ${ISO_ROOT}/grub4dos
    fi
    cp -va /usr/lib/syslinux/memdisk ${ISO_ROOT}/isolinux

    mkdir -p ${ISO_ROOT}/EFI/BOOT
    mkdir -p ${ISO_ROOT}/EFI/GRUB
    mkdir -p ${ISO_ROOT}/boot
    {
        cp -va MaxDOS.img ${ISO_ROOT}/boot &&
        cat ${grub_libdir}/i386-pc/lnxboot.img core.img >${ISO_ROOT}${GRUB2_PREFIX}/g2ldr &&
        test -f grubx64.efi && mv -v grubx64.efi ${ISO_ROOT}${GRUB2_PREFIX} &&
        mv -v core.img ${ISO_ROOT}${GRUB2_PREFIX}/ &&
        test -f grub2.cfg && cp -av grub2.cfg ${ISO_ROOT}${GRUB2_PREFIX}/grub.cfg &&
        ln -f ${ISO_ROOT}${GRUB2_PREFIX}/grubx64.efi ${ISO_ROOT}/EFI/GRUB/grubx64.efi &&
        ln -f ${ISO_ROOT}${GRUB2_PREFIX}/grubx64.efi ${ISO_ROOT}/EFI/BOOT/BOOTX64.efi  &&
        cp -av shellx64.efi ${ISO_ROOT}
    }|| exit 1
}

copy_to
